<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>问卷管理</el-breadcrumb-item>
      <el-breadcrumb-item>答者统计</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 用户列表主体部分 -->
    <!-- 卡片视图区 -->
    <el-card>
      <el-row :gutter="25">
        <el-col :span="10">
          <!-- 搜索区域 -->
          <el-input
            placeholder="请输入搜索内容"
            v-model="queryInfo.query"
            clearable
            @clear="getAnsweredList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getAnsweredList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="ViewStatistics">数据总览</el-button>
        </el-col>
      </el-row>

      <!-- 用户列表 border边框 stripe隔行变色-->
      <el-table :data="answeredList" border="" stripe="">
        <!-- 索引列 -->
        <el-table-column type="index"></el-table-column>
        <el-table-column label="答者名" prop="name"></el-table-column>
        <el-table-column label="答题时间" prop="ansTime"></el-table-column>

        <el-table-column label="答题情况">
          <template slot-scope="scope">
            <!-- 查看 -->
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="ViewAnswer(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <div>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[1, 2, 5, 100]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  created() {
    this.getAnsweredList();
  },

  data() {
    return {
      ...this.$route.query,
      //查询信息实体
      queryInfo: {
        query: "", //查询信息
        pageNum: 1, //当前页
        pageSize: 5, //每一页最大数
      },
      answeredList: [], //群组列表
      total: 0, //总记录数
    };
  },

  methods: {
    async ViewStatistics() {
      // console.log(this.$route.query)
      let name = window.sessionStorage.getItem("name");
      let qid = this.$route.query.id;

      let pathInfo = this.$router.resolve({
        path: "/statistics",
        query: {
          name: name,
          qid: qid,
        },
      });
      window.open(pathInfo.href, "_blank");
    },
    //获取所有已经回答的答者信息
    async getAnsweredList() {
      const id = this.id + "";
      await this.$http({
        method: "post",
        url: "/questionnaireAnsweredY",
        data: {
          query: this.queryInfo.query,
          pageNum: this.queryInfo.pageNum,
          pageSize: this.queryInfo.pageSize,
          qid: id,
        },
      }).then((response) => {
        this.answeredList = response.data.data.list;
        this.total = response.data.data.total;
      });
      console.log(this.answeredList);
    },

    //根据user_name,qid查看某一特定问卷的答题情况
    async ViewAnswer(para) {
      const { data: res } = await this.$http.post(
        "queryQuestionnaireByQidN?qId=" + para.qid
      );

      if (res.code == "666") {
        console.log(res);
        let pathInfo = this.$router.resolve({
          path: "/answerq",
          query: {
            name: para.name,
            qid: para.qid,
            title: res.data[0].title,
            content: res.data[0].content,
            answer: para.answer,
            checking: true,
          },
        });
        window.open(pathInfo.href, "_blank");
      } else {
        alert("查询失败");
      }
    },

    //最大数
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getAnsweredList();
    },

    //pageNum触发的动作
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getAnsweredList();
    },
  },
};
</script>

<style lang="less" scoped>
/* 面包屑样式 */
.el-breadcrumb {
  margin-bottom: 15px;
  font-size: 17px;
}
</style>